<div id="content">
    <h3><a href="?p=home&t=student">Geoplaza</a><a href="?p=home&t=student"> &rsaquo;&rsaquo; studenten</a> &rsaquo;&rsaquo; Profiel aanmaken</h3>
    <?php
    
    require_once(CONFIG_PATH.'appvars.php');
    require_once(CONFIG_PATH.'config.php');

    // Connect to the database
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

    if (isset($_POST['submit'])) {
        // Grab the profile data from the POST
        $voornaam = mysqli_real_escape_string($dbc, trim($_POST['voornaam']));
        $achternaam = mysqli_real_escape_string($dbc, trim($_POST['achternaam']));
        $geslacht = mysqli_real_escape_string($dbc, trim($_POST['geslacht']));
        $geboortedag = $_POST['geboortedag'];
        $geboortemaand = $_POST['geboortemaand'];
        $geboortejaar = $_POST['geboortejaar'];
        $geboortedatum = $geboortejaar . "-" . $geboortemaand . "-" . $geboortedag;
        $adres = mysqli_real_escape_string($dbc, trim($_POST['adres']));
        $postcode = mysqli_real_escape_string($dbc, trim($_POST['postcode']));
        $woonplaats = mysqli_real_escape_string($dbc, trim($_POST['woonplaats']));
        $province = mysqli_real_escape_string($dbc, trim($_POST['province']));
        $telefoon = mysqli_real_escape_string($dbc, trim($_POST['telefoon']));
        $biography = mysqli_real_escape_string($dbc, trim($_POST['biography']));

        $opleiding = mysqli_real_escape_string($dbc, trim($_POST['opleiding']));
        $niveau = mysqli_real_escape_string($dbc, trim($_POST['niveau']));
        $school = mysqli_real_escape_string($dbc, trim($_POST['school']));
        $stagedag = $_POST['stagedag'];
        $stagemaand = $_POST['stagemaand'];
        $stagejaar = $_POST['stagejaar'];
        $stagedatum = $stagejaar . "-" . $stagemaand . "-" . $stagedag;
        $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
        $wachtwoord = mysqli_real_escape_string($dbc, trim($_POST['wachtwoord']));

        $new_picture = mysqli_real_escape_string($dbc, trim($_FILES['new_picture']['name']));
        $new_picture_type = $_FILES['new_picture']['type'];
        $new_picture_size = $_FILES['new_picture']['size'];
        list($new_picture_width, $new_picture_height) = getimagesize($_FILES['new_picture']['tmp_name']);
        $error = false;

        // Validate and move the uploaded picture file, if necessary
        if (!empty($new_picture)) {
            if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||
                    ($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) &&
                    ($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {
                if ($_FILES['new_picture']['error'] == 0) {
                    // Move the file to the target upload folder
                    $target = MM_UPLOADPATH . basename($new_picture);
                }
            } else {
                // The new picture file is not valid, so delete the temporary file and set the error flag
                @unlink($_FILES['new_picture']['tmp_name']);
                $error = true;
                echo '<p class="error">Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .
                ' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.</p>';
            }
        }

        // Update the profile data in the database
        if (!$error) {
            if (!empty($voornaam) && !empty($achternaam) && !empty($geslacht) && !empty($geboortedatum) && !empty($woonplaats) && !empty($province)) {
                // Only set the picture column if there is a new picture
                if (!empty($new_picture)) {
                    $query = "UPDATE studentprofiel SET voornaam = '$voornaam', achternaam = '$achternaam', geslacht = '$geslacht', " .
                " geboortedatum = '$geboortedatum', adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', provencie = '$province', telefoon = '$telefoon', biography = '$biography', foto = '$new_picture', opleiding = '$opleiding', niveau = '$niveau', school = '$school', stagedate = '$stagedatum', email = '$email' WHERE studentID = '" . $_SESSION['user_id'] . "'";

                } else {
                    $query = "UPDATE studentprofiel SET voornaam = '$voornaam', achternaam = '$achternaam', geslacht = '$geslacht', " .
                " geboortedatum = '$geboortedatum', adres = '$adres', postcode = '$postcode', woonplaats = '$woonplaats', provencie = '$province', telefoon = '$telefoon', biography = '$biography', opleiding = '$opleiding', niveau = '$niveau', school = '$school', stagedate = '$stagedatum', email = '$email' WHERE studentID = '" . $_SESSION['user_id'] . "'";

                }
                mysqli_query($dbc, $query);

                // Confirm success with the user
                echo '<p>Your profile has been successfully updated. Would you like to <a href="?p=viewprofiel&t=student">view your profile</a>?</p>';

                mysqli_close($dbc);
                exit();
            } else {
                echo '<p class="error">You must enter all of the profile data (the picture is optional).</p>';
            }
        }
    } // End of check for form submission

    mysqli_close($dbc);
    ?>

   <div id="signup">
      <form enctype="multipart/form-data" method="post" action="?p=makeprofiel&t=student">

        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />

            <fieldset><label for="voornaam">Voornaam</label>
                <input name="voornaam" id="voornaam" type="text" /></fieldset>

            <fieldset><label for="achternaam">Achternaam</label>
                <input name="achternaam" id="achternaam" type="text" /></fieldset>

            <fieldset><label for="geslacht">Ik ben een</label>
                <select name="geslacht">
                    <option value="man">Man</option>
                    <option value="vrouw">Vrouw</option>
                </select>
            </fieldset>

            <fieldset><label for="dob">Geboortedatum</label>
                <select name="geboortedag">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                    <option>13</option>
                    <option>14</option>
                    <option>15</option>
                    <option>16</option>
                    <option>17</option>
                    <option>18</option>
                    <option>19</option>
                    <option>20</option>
                    <option>21</option>
                    <option>22</option>
                    <option>23</option>
                    <option>24</option>
                    <option>25</option>
                    <option>26</option>
                    <option>27</option>
                    <option>28</option>
                    <option>29</option>
                    <option>30</option>
                    <option>31</option>
                </select>
                <select name="geboortemaand">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                </select>
                <select name="geboortejaar">
                    <?php
                    $datum = date('Y');
                    $start = $datum - 100;
                    while ($start <= $datum) {
                    ?>
                        <option>
                        <?php
                        print ($start);
                        $start++;
                        ?>
                    </option>
                    <?php }
                    ?>
                </select>
            </fieldset>

            <fieldset><label for="adres">Adres</label>
                <input name="adres" id="adres" type="text"/></fieldset>

            <fieldset><label for="postcode">Postcode</label>
                <input name="postcode" id="postcode" type="text"/></fieldset>

            <fieldset><label for="woonplaats">Woonplaats</label>
                <input name="woonplaats" id="woonplaats" type="text"/></fieldset>

            <fieldset><label for="province">Provincie</label>
                <select name="province">
                    <option value="Groningen">Groningen</option>
                    <option value="Friesland">Friesland</option>
                    <option value="Drenthe">Drenthe</option>
                    <option value="Overijssel">Overijssel</option>
                    <option value="Flevoland">Flevoland</option>
                    <option value="Gelderland">Gelderland</option>
                    <option value="Utrecht">Utrecht</option>
                    <option value="Noord-Holland">Noord-Holland</option>
                    <option value="Zuid-Holland">Zuid-Holland</option>
                    <option value="Zeeland">Zeeland</option>
                    <option value="Noord-Barabant">Noord-Barabant</option>
                    <option value="Limburg">Limburg</option>
                </select>
            </fieldset>

            <fieldset><label for="telefoon">Telefoon</label>
                <input name="telefoon" id="telefoon" type="text" /></fieldset>

            <fieldset><label for="bio">Biography</label>
                <textarea name="bio" id="bio"></textarea></fieldset>
<fieldset>
          <input type="hidden" name="old_picture" value="<?php if (!empty($old_picture))
                            echo $old_picture; ?>" />
                 <label for="new_picture">Picture:</label>
                 <input type="file" id="new_picture" name="new_picture" />
            <?php
                        if (!empty($old_picture)) {
                            echo '<img class="profile" src="' . MM_UPLOADPATH . $old_picture . '" alt="Profile Picture" />';
                        } ?>

                         </fieldset>

 <h3>Opleiding</h3>
            <hr>
            <fieldset><label for="opleiding">Huidige opleiding </label>
                <input name="opleiding" id="opleiding" type="text" /></fieldset>

            <fieldset class="chk"><label for="niveau">Niveau</label>
                <ul>
                    <li><input type="radio" name="niveau" value="MBO" checked>MBO</li>
                    <li><input type="radio" name="niveau" value="HBO">HBO</li>
                    <li><input type="radio" name="niveau" value="WO">WO</li>
                </ul>
            </fieldset>
            <fieldset><label for="school">Naam school</label>
                <input name="school" id="school" type="text" /></fieldset>
            <fieldset><label for="stagedatum">Voor de stage ik beschikbaar</label>
                <select name="stagedag">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                    <option>13</option>
                    <option>14</option>
                    <option>15</option>
                    <option>16</option>
                    <option>17</option>
                    <option>18</option>
                    <option>19</option>
                    <option>20</option>
                    <option>21</option>
                    <option>22</option>
                    <option>23</option>
                    <option>24</option>
                    <option>25</option>
                    <option>26</option>
                    <option>27</option>
                    <option>28</option>
                    <option>29</option>
                    <option>30</option>
                    <option>31</option>
                </select>
                <select name="stagemaand">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                    <option>11</option>
                    <option>12</option>
                </select>
                <select name="stagejaar">
                    <?php
                    $datum = date('Y');
                    $start = $datum + 4;
                    while ($datum <= $start) {
                    ?>
                        <option>
                        <?php
                        print ($datum);
                        $datum++;
                        ?>
                    </option>
                    <?php }
                    ?>
                </select>
            </fieldset>

            <h3>Inlog gegevens</h3>
            <hr>

            <fieldset><label for="email">Gebruikersnaam (je email)</label>
                <input name="email" id="email" type="text" /></fieldset>

            <fieldset><label for="wachtwoord">Wachtwoord</label>
                <input name="wachtwoord" id="wachtwoord" type="password" /></fieldset>

            <fieldset><label for="rewachtwoord">Bevestig wachtwoord</label>
                <input name="rewachtwoord" id="rewachtwoord" type="password" /></fieldset>

            <fieldset class="nolabel">
                <input type="submit" id="enrol" value="Enroll" disabled="disabled"  />
            </fieldset>
        </form>
    </div>
</div>